# -*-coding:utf-8-*-

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
import logging

username = "xxx"
password = "xx"

driver = webdriver.Chrome()
driver.maximize_window()
# 超时设置为5秒
driver.implicitly_wait(5)

driver.get("https://www.rabbitpre.com/")
signin_btn = driver.find_element_by_css_selector('#g-j-signin-btn')
signin_btn.click()

login_iframe_window = driver.find_element_by_css_selector('#sso')

driver.switch_to.frame(login_iframe_window)

lw_account_tab = driver.find_element_by_css_selector('span[data-type="tuzhan"]')
lw_account_tab.click()

lw_input_user = driver.find_element_by_css_selector('[placeholder="请输入你的登录账户"]')
lw_input_user.send_keys(username)
logging.info('输入了用户名')

lw_input_pw = driver.find_element_by_css_selector('[placeholder="请输入密码"]')
lw_input_pw.send_keys(password)
logging.info('输入了密码')

lw_login_button = driver.find_element_by_css_selector('button.g-btn.login-btn.do-btn')
lw_login_button.click()
logging.info('点击了登录按钮')


reg_err_tip = lambda x: x.find_element_by_css_selector('div.login-form span.error-tips')
try:
    reg_err_tip = WebDriverWait(driver, 5, 1).until_not(reg_err_tip)
except TimeoutException:
    logging.error('登录失败，接口返回: {}.format(reg_err_tip(driver).text)')
    driver.save_screenshot("login.png")
else:
    logging.info('登录成功')

driver.close()

driver.quit()
